package com.example.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.project.common.Result;
import com.example.project.dto.request.ProductReviewRequest;
import com.example.project.dto.request.ProductSearchRequest;
import com.example.project.dto.response.ProductResponse;
import com.example.project.dto.response.ProductReviewResponse;
import com.example.project.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    /**
     * 获取商品详情
     */
    @GetMapping("/{id}")
    public Result<ProductResponse> getProductDetail(@PathVariable Long id) {
        return productService.getProductDetail(id);
    }

    /**
     * 搜索商品
     */
    @GetMapping
    public Result<Page<ProductResponse>> searchProducts(ProductSearchRequest request) {
        return productService.searchProducts(request);
    }

    /**
     * 获取商品分类
     */
    @GetMapping("/categories")
    public Result<?> getCategories() {
        return productService.getCategories();
    }

    /**
     * 收藏商品
     */
    @PostMapping("/{id}/favorite")
    public Result<?> favoriteProduct(@PathVariable Long id, 
                                   @RequestAttribute Long userId) {
        return productService.favoriteProduct(id, userId);
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/{id}/favorite")
    public Result<?> unfavoriteProduct(@PathVariable Long id, 
                                     @RequestAttribute Long userId) {
        return productService.unfavoriteProduct(id, userId);
    }

    /**
     * 获取收藏列表
     */
    @GetMapping("/favorites")
    public Result<Page<ProductResponse>> getFavorites(
            @RequestAttribute Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return productService.getFavorites(userId, pageNum, pageSize);
    }

    /**
     * 检查商品是否已收藏
     */
    @GetMapping("/{id}/favorite")
    public Result<Boolean> checkFavorite(@PathVariable Long id, 
                                       @RequestAttribute Long userId) {
        return productService.checkFavorite(id, userId);
    }

    /**
     * 创建商品评价
     */
    @PostMapping("/{id}/reviews")
    public Result<?> createReview(@PathVariable("id") Long productId,
                                @RequestBody @Valid ProductReviewRequest request,
                                @RequestAttribute Long userId) {
        request.setProductId(productId);
        return productService.createReview(request, userId);
    }

    /**
     * 商家回复评价
     */
    @PostMapping("/reviews/{id}/reply")
    public Result<?> replyReview(@PathVariable("id") Long reviewId,
                                @RequestBody String reply,
                                @RequestAttribute Long userId) {
        return productService.replyReview(reviewId, reply, userId);
    }

    /**
     * 获取商品评价列表
     */
    @GetMapping("/{id}/reviews")
    public Result<Page<ProductReviewResponse>> getProductReviews(
            @PathVariable("id") Long productId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return productService.getProductReviews(productId, pageNum, pageSize);
    }

    /**
     * 更新商品库存
     */
    @PutMapping("/{id}/stock")
    public Result<?> updateStock(@PathVariable Long id, 
                               @RequestParam Integer quantity,
                               @RequestParam String type) {
        return productService.updateStock(id, quantity, type);
    }

    /**
     * 获取商品统计数据
     */
    @GetMapping("/{id}/stats")
    public Result<?> getProductStats(@PathVariable Long id) {
        return productService.getProductStats(id);
    }

    /**
     * 获取热门商品
     */
    @GetMapping("/hot")
    public Result<?> getHotProducts(@RequestParam(defaultValue = "10") Integer limit) {
        return productService.getHotProducts(limit);
    }

    /**
     * 获取新品推荐
     */
    @GetMapping("/new")
    public Result<?> getNewProducts(@RequestParam(defaultValue = "10") Integer limit) {
        return productService.getNewProducts(limit);
    }
} 